#include "pch.hpp"

#include "SimpleParticle.h"

void SimpleParticle::load(int count, sf::Image &img, sf::Vector2f pos)
{
	sprite.SetImage(img);
	sprite.SetCenter(img.GetWidth()/2.0f, img.GetHeight()/2.0f);

	Particle p;
	p.position = pos;
	p.life = 1;
	sf::Randomizer rr;
	rr.SetSeed(time(0));
	while(count--)
	{
		p.rotation_speed = rr.Random(-180.0f, 180.0f);
		p.rotation = rr.Random(-180.0f, 180.0f);
		p.velocity = sf::Vector2f(0, 1);
		sfm::Rotate(p.velocity, rr.Random(-180.0f, 180.0f));
		p.velocity *= rr.Random(1.0f, 30.0f);
		
		particles.push_back(p);
	}
}

void SimpleParticle::draw()
{
	for(int i=0; i<particles.size(); i++)
	{
		if(particles[i].life > 0)
		{
			sprite.SetColor(sf::Color(255,255,255,255*particles[i].life));
			sprite.SetPosition(particles[i].position);
			app.Draw(sprite);
		}
	}
}

bool SimpleParticle::step()
{
	bool any_alive = false;

	for(int i=0; i<particles.size(); i++)
	{
		Particle &p = particles[i];
		p.position += p.velocity*1.0f/60.0f;
		p.rotation += p.rotation_speed*1.0f/60.0f;
		p.life -= 1.0f/60.0f;

		if(p.life>0) any_alive = true;
	}

	return any_alive;
}